﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Orders;
using Invoicing.Command.Orders;
using Utils;
namespace Invoicing.Logic.Orders
{
    public class BookOrderLogic
    {
        public static BookOrderModel NewOrder(BookOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new InvoicingException("没有内容");
                if (order.CustomerID == 0)
                    throw new InvoicingException("请选择一个客户");
                var customer = Members.CustomerLogic.GetModel(order.CustomerID);
                if (customer == null)
                    throw new InvoicingException("无效的客户");

                var sta = order.Items.GroupBy(p => new { p.ProductID, p.MaterialID, p.ProcessID, p.Size }).Select(p => new { p.Key, Ct = p.Count() });
                if (sta.Any(p => p.Ct > 1))
                    throw new InvoicingException("同一张单据中不充许有相同的产品记录");

                foreach (var it in order.Items)
                {
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (it.MaterialID == 0)
                        throw new ApplicationException("无效的材料");
                    if (it.ProcessID == 0)
                        throw new ApplicationException("无效的加工");
                    if (string.IsNullOrEmpty(it.Size))
                        throw new ApplicationException("无效的规格");
                }

                order.OrderStatus = (int)OrderStatus.New;
                order.CreatedOn = DateTime.Now;
                return BookOrderCommand.NewOrder(order);
            }
            return null;
            
        }

        public static void UpdateOrder(BookOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new InvoicingException("没有内容");
                if (order.CustomerID == 0)
                    throw new InvoicingException("请选择一个客户");
                var customer = Members.CustomerLogic.GetModel(order.CustomerID);
                if (customer == null)
                    throw new InvoicingException("无效的客户");

                var sta = order.Items.GroupBy(p => new { p.ProductID, p.MaterialID, p.ProcessID, p.Size }).Select(p => new { p.Key, Ct = p.Count() });
                if (sta.Any(p => p.Ct > 1))
                    throw new InvoicingException("同一张单据中不充许有相同的产品记录");

                foreach (var it in order.Items)
                {
                    var product = Products.ProductLogic.GetModel(it.ProductID);
                    if (product == null)
                        throw new ApplicationException("无效的产品");
                    it.Product = product;
                    if (it.MaterialID == 0)
                        throw new ApplicationException("无效的材料");
                    if (it.ProcessID == 0)
                        throw new ApplicationException("无效的加工");
                    if (string.IsNullOrEmpty(it.Size))
                        throw new ApplicationException("无效的规格");
                }

                order.CreatedOn = DateTime.Now;
                BookOrderCommand.UpdateOrder(order);
            }
        }
        /// <summary>
        /// 查询单据列表
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static BookOrderCollection GetOrders(BookOrderQuery sea)
        {
            return BookOrderCommand.GetOrders(sea);
        }
        /// <summary>
        /// 查询单张单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public static BookOrderModel GetOrder(string orderNo)
        {
            return BookOrderCommand.GetOrder(orderNo);
        }
        /// <summary>
        /// 撤消交易单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public static void DeleteOrder(string orderNo)
        {
            BookOrderCommand.DeleteOrder(orderNo);
        }
    }
}
